<?php

namespace app\api\controller;

use app\admin\model\money\Change;
use app\admin\model\product\Category;
use app\admin\model\product\Listo;
use app\admin\model\product\Lists;
use app\admin\model\product\Order;
use app\admin\model\product\Sku;
use app\admin\model\product\Value;
use app\admin\model\user\Finance;
use app\common\api\controller\Relation;
use app\common\model\Member;
use app\common\model\MemberFinance;
use think\Db;
use think\Request;
use app\common\controller\Api;
use think\Validate;

class Moneychange extends Api
{
    protected static $model = null;
    protected $noNeedRight = '*';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new Change();
    }

    /**
     * 转币
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tfReply()
    {
        if (!request()->isPost()) {
            $this->error('非法操作！');
        }
        $money = input('money', 0);                                  //转入用户
        if ($money <= 0) {
            $this->error('转币额度必须大于零！');
        }
//        $password = input('password');
        $userId = $this->auth->getUser()->id;
        $finance = Finance::where('user_id', $userId)->find();

        // 为1表示静态现金转储存币，为2表示动态现金转储存币，为3表示储存币转静态现金，为4表示储存币转动态现金
        $tftype = input('tftype', 1);
        if ($tftype == 1 && $finance->hasmoneys < $money) {
            $this->error('余额不足！');
        }

        if ($tftype == 2 && $finance->hasmoneyd < $money) {
            $this->error('余额不足！');
        }

        if (in_array($tftype, [3, 4]) && $finance->hasmoney < $money) {
            $this->error('余额不足！');
        }

        switch ($tftype) {
            //静态现金兑换储存币
            case 1:
                $outfiled = 'hasmoneys';
                $infiled = 'hasmoney';
                $outmoneytype = 3;
                $inmoneytype = 6;
                $outmoney = $money;
                $inmoney = $money;
                break;
            //动态现金兑换储存币
            case 2:
                $outfiled = 'hasmoneyd';
                $infiled = 'hasmoney';
                $outmoneytype = 4;
                $inmoneytype = 6;
                $outmoney = $money;
                $inmoney = $money;
                break;
            //储存币兑换静态现金
            case 3:
                $outfiled = 'hasmoney';
                $infiled = 'hasmoneys';
                $outmoneytype = 6;
                $inmoneytype = 3;
                $outmoney = $money;
                $inmoney = $money;
                break;
            //储存币兑换动态现金
            case 4:
                $outfiled = 'hasmoney';
                $infiled = 'hasmoneyd';
                $outmoneytype = 6;
                $inmoneytype = 4;
                $outmoney = $money;
                $inmoney = $money;
                break;
            default:
                $this->error('非法操作！');
                break;
        }

        if ($finance->$outfiled < $money) {
            $this->error('转币金额大于账户余额');
        }

//        if ($bonusRole['min_tf'] > $money) {
//            $this->error('转币金额小于最小转币金额');
//        }

//        if ($money % $multipleWd) {
//            $this->error("转币金额必须是{$multipleWd}的整数倍");
//        }

        $sql1 = "update fa_user_finance  set {$outfiled} = {$outfiled}-{$outmoney} where user_id={$userId}";
        switch ($inmoneytype) {
            case 3:
                $sql2 = "update fa_user_finance  set {$infiled} = {$infiled}+{$inmoney},allmoneys = allmoneys + {$inmoney} where user_id={$userId}";
                break;
            case 4:
                $sql2 = "update fa_user_finance  set {$infiled} = {$infiled}+{$inmoney},allmoneyd = allmoneyd + {$inmoney} where user_id={$userId}";
                break;
            case 6:
                $sql2 = "update fa_user_finance  set {$infiled} = {$infiled}+{$inmoney},allmoney = allmoney + {$inmoney} where user_id={$userId}";
                break;
        }

        Db::startTrans();
        try {
            Db::execute($sql1);
            Db::execute($sql2);

            $finance = Finance::where('user_id', $userId)->find();
            $change_type = 11;
            $orderNum = create_order($userId);
            $mdata1 = [
                'order_num' => $orderNum,
                'record_type' => 0,
                'change_type' => $change_type,
                'moneytype' => $outmoneytype,
                'money' => $outmoney,
                'hasmoney' => $finance->$outfiled,
//                'remark' => $tag,
                'user_id' => $userId,
                'targetuserid' => $userId
            ];
            Change::create($mdata1);

            $orderNum = create_order($userId);
            $mdata2 = [
                'order_num' => $orderNum,
                'record_type' => 1,
                'change_type' => $change_type,
                'moneytype' => $inmoneytype,
                'money' => $inmoney,
                'hasmoney' => $finance->$infiled,
//                'remark' => $tag,
                'user_id' => $userId,
                'targetuserid' => $userId
            ];
            Change::create($mdata2);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('转币失败');
        }

        $this->success('转币成功');
    }
}